/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BuktiTransaksiForm.java
 *
 * Created on 13 Nov 11, 9:11:51
 */
package BuktiTransaksi;

import Koneksi.koneksi;
import MenuUtamaSistem.Login;
import MenuUtamaSistem.MenuUtamaController;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Ignacio
 */
public class BuktiTransaksiForm extends javax.swing.JDialog {

    /** Creates new form BuktiTransaksiForm */
    public BuktiTransaksiForm() {
        initComponents();
        cariTransaksiText.setText("MAFYJ-");
//        userText.setText(Login.getLog().getUser());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        formBukti = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cariTransaksiText = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        buktiTransaksiTable = new javax.swing.JTable();
        backButton = new javax.swing.JButton();
        printBuktiButton = new javax.swing.JButton();
        logo = new javax.swing.JLabel();
        logoLabel = new javax.swing.JLabel();
        userLabel = new javax.swing.JLabel();
        userText = new javax.swing.JLabel();
        batalButton = new javax.swing.JButton();
        BG = new javax.swing.JLabel();

        setTitle("Bukti Transaksi - SP3EF");

        jPanel1.setBackground(new java.awt.Color(153, 204, 255));
        jPanel1.setLayout(null);
        jPanel1.add(jSeparator1);
        jSeparator1.setBounds(0, 90, 590, 10);

        formBukti.setFont(new java.awt.Font("Vani", 1, 24));
        formBukti.setText("Bukti Transaksi");
        jPanel1.add(formBukti);
        formBukti.setBounds(10, 30, 280, 30);

        jLabel2.setText("No Transaksi");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(20, 120, 80, 20);

        cariTransaksiText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cariTransaksiTextActionPerformed(evt);
            }
        });
        jPanel1.add(cariTransaksiText);
        cariTransaksiText.setBounds(120, 120, 140, 20);

        buktiTransaksiTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "No Transaksi", "Waktu", "Nomor", "Nominal", "Harga"
            }
        ));
        jScrollPane1.setViewportView(buktiTransaksiTable);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(20, 160, 500, 150);

        backButton.setText("Kembali Ke Menu Utama");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        jPanel1.add(backButton);
        backButton.setBounds(30, 340, 160, 23);

        printBuktiButton.setText("Cetak");
        printBuktiButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printBuktiButtonActionPerformed(evt);
            }
        });
        jPanel1.add(printBuktiButton);
        printBuktiButton.setBounds(453, 340, 80, 23);

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BuktiTransaksi/HP.png"))); // NOI18N
        jPanel1.add(logo);
        logo.setBounds(460, 0, 100, 90);

        logoLabel.setText("@MAFY com 2011");
        jPanel1.add(logoLabel);
        logoLabel.setBounds(240, 370, 120, 20);

        userLabel.setText("User");
        jPanel1.add(userLabel);
        userLabel.setBounds(360, 100, 70, 20);
        jPanel1.add(userText);
        userText.setBounds(420, 100, 90, 20);

        batalButton.setText("Batal");
        batalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalButtonActionPerformed(evt);
            }
        });
        jPanel1.add(batalButton);
        batalButton.setBounds(360, 340, 80, 23);

        BG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MenuUtamaSistem/BG.jpg"))); // NOI18N
        jPanel1.add(BG);
        BG.setBounds(0, 0, 564, 400);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 565, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
//        BuktiTransaksi bukti = new BuktiTransaksi();
//
//    public void setNoTransaksi() {
//        bukti.setNo_transaksi_penj(cariTransaksiText.getText());
//
//    }

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        Login.getMain().setVisible(true);
    }//GEN-LAST:event_backButtonActionPerformed

    private void cariTransaksiTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cariTransaksiTextActionPerformed
        // TODO add your handling code here:
        String input = cariTransaksiText.getText();

//        if (input == null) {
//            JOptionPane.showMessageDialog(rootPane, "Transaksi Tak Ditemukan");
//        } else {
        try {
            try {
//                BuktiTransaksi.setNo_transaksi_penj(input);
                List<BuktiTransaksi> bukti = BuktiTransaksiController.getTransaksiController().getNoTransaksiPenj(input);
                buktiTabelModel model = new buktiTabelModel(bukti);
                buktiTransaksiTable.setModel(model);
                if (buktiTransaksiTable.getRowCount() == 0) {
                    JOptionPane.showMessageDialog(null, "Transaksi Tidak ditemukan");
                } else {
                    JOptionPane.showMessageDialog(null, "Transaksi  ditemukan");
                }

            } catch (FileNotFoundException ex) {
                Logger.getLogger(BuktiTransaksiForm.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Transaksi Tidak ditemukan");
            } catch (IOException ex) {
                Logger.getLogger(BuktiTransaksiForm.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Transaksi Tidak ditemukan");
            }
        } catch (SQLException ex) {
            Logger.getLogger(BuktiTransaksiForm.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Transaksi Tidak ditemukan");
        }
//        }
    }//GEN-LAST:event_cariTransaksiTextActionPerformed

    private void printBuktiButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printBuktiButtonActionPerformed
        int print = JOptionPane.showConfirmDialog(this, "Yakin akan di cetak?", "Question", JOptionPane.YES_NO_OPTION);
        if (print == 0) {
            try {
                // TODO add your handling code here:
                Connection con = null;
                String reportSource = "";
                con = koneksi.getDBConnection();


                try {
//                String kode= JOptionPane.showInputDialog("masukkan no transaksi :");
                    reportSource = "./Report/BuktiTransaksi.jrxml";
                    JasperReport jr = JasperCompileManager.compileReport(reportSource);
                    Map<String, Object> params = new HashMap<String, Object>();
                    params.put("notrans", cariTransaksiText.getText());
                    JasperPrint jasper = JasperFillManager.fillReport(jr, params, con);
                    JasperViewer.viewReport(jasper, false);

                } catch (JRException ex) {
                    Logger.getLogger(BuktiTransaksiForm.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (SQLException ex) {
                Logger.getLogger(BuktiTransaksiForm.class.getName()).log(Level.SEVERE, null, ex);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(BuktiTransaksiForm.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(BuktiTransaksiForm.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_printBuktiButtonActionPerformed

    private void batalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalButtonActionPerformed
        // TODO add your handling code here:
        int pilih = JOptionPane.showConfirmDialog(this, "Apakah anda yakin ingin membatalkan?", "Questios", JOptionPane.YES_NO_OPTION);
        if (pilih == 0) {
            cariTransaksiText.setText("MAFYJ-");
            buktiTransaksiTable.setModel(new javax.swing.table.DefaultTableModel(
                    new Object[][]{
                        {null, null, null, null, null},
                        {null, null, null, null, null},
                        {null, null, null, null, null},
                        {null, null, null, null, null}
                    },
                    new String[]{
                        "No Transaksi", "Waktu", "Nomor", "Nominal", "Harga"
                    }));
        }
    }//GEN-LAST:event_batalButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new BuktiTransaksiForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel BG;
    private javax.swing.JButton backButton;
    private javax.swing.JButton batalButton;
    private javax.swing.JTable buktiTransaksiTable;
    private javax.swing.JTextField cariTransaksiText;
    private javax.swing.JLabel formBukti;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel logo;
    private javax.swing.JLabel logoLabel;
    private javax.swing.JButton printBuktiButton;
    private javax.swing.JLabel userLabel;
    private javax.swing.JLabel userText;
    // End of variables declaration//GEN-END:variables
}
